<template>
  <el-container class="home-container">
    <!-- 头部区域 -->
    <el-header>
      <div class="logo-container">
        <img class="logo" src="@/assets/logo.png" />
        <span>Vue电商管理系统</span>
      </div>
      <el-button @click="logout" type="success">退出</el-button>
    </el-header>
    <el-container>
      <!-- 导航栏区域 -->
      <el-aside :width="isCollapse? '64px' : '200px'">
          <!-- 控制折叠按钮 -->
          <div class="toggle-button" @click="toggleCollapse">|||</div>
        <el-menu
            :default-active="this.$route.path"
          class="el-menu-vertical-demo"
          background-color="#333744"
          text-color="#fff"
          active-text-color="#409EFF"
          unique-opened
          :collapse="isCollapse"
          :collapse-transition="false"
          router
        >
          <!-- 一级菜单 -->
          <el-submenu :index='item.path' v-for="(item, i) in menuList" :key="item.id">
            <!-- 一级菜单的图标与文本 -->
            <template slot="title">
              <i :class="iconList[i]"></i>
              <span>{{item.authName}}</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item v-for="subItem in item.children" :key="subItem.id" :index="'/'+subItem.path">
              <template slot="title">
                <i class="el-icon-menu"></i>
                <span>{{subItem.authName}}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 主界面区域 -->
      <el-main>
          <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
    data() {
        return {
            // 导航栏列表数据数据
            menuList: [],
            // 导航栏图标
            iconList: [
                'el-icon-user-solid',
                'el-icon-s-check',
                'el-icon-s-goods',
                'el-icon-s-shop',
                'el-icon-s-data'
            ],
            // 导航栏是否折叠
            isCollapse: false
        }
    },
  methods: {
    logout() {
      window.sessionStorage.clear();
      this.$message.info("退出登录成功");
      this.$router.push("/login");
    },
    // 获取所有的菜单
    getMenuList() {
        // 解决this指向问题，this在方法中指向本页面，请求后会指向请求，导致无法使用变量
        var _this = this;
       this.$http.get('menus').then(function(res){
           if(res.data.meta.status !== 200){
               alert(res.data.meta.msg)
           }else{
               _this.menuList = res.data.data;
           }
           console.log(res.data.data)
       },function(res){
           alert("获取菜单列表失败")
           console.log(res)
       })
    },
    // 控制导航栏的折叠与展开
    toggleCollapse() {
        this.isCollapse = !this.isCollapse;
    }
  },
  created() {
      this.getMenuList();
  }
};
</script>

<style scoped>
/* 组件名称为类名 */
.el-header {
  background-color: #373d41;
  display: flex;
  justify-content: space-between;
  /* 原先按钮上下填满父组件，添加此属性后恢复原大小 */
  align-items: center;
  /* 文本颜色 */
  color: #ffffff;
}

.el-aside {
  background-color: #333744;
}

/* 解决子菜单比父菜单多1px */
.el-menu{
    border-right-width: 0;
}

.el-main {
  background-color: #eaedf1;
}

/* 页面充满全屏 */
.home-container {
  height: 100%;
}

.logo-container {
  display: flex;
  align-items: center;
}

.logo {
  width: 50px;
  height: 50px;
}

.toggle-button {
    background-color: #4A5064;
    font-size: 10px;
    line-height: 24px;
    color: #ffffff;
    text-align: center;
    letter-spacing: 0.2em;
    /* 鼠标放在元素边界时光标变成小手 */
    cursor: pointer;
}
</style>

// 通过axios请求拦截器添加token，保证拥有数据的权限